package com.ruiyian.bridge.opreations.controller;

import com.ruiyian.bridge.opreations.domain.TDeviceWarn;
import com.ruiyian.bridge.opreations.service.MonitorDataExcelService;
import com.ruiyian.bridge.project.domain.vo.DataAnalysisVo;
import com.ruiyian.bridge.project.domain.vo.MonitorDataExcelBo;
import com.ruiyian.bridge.project.enums.DeviceTypeEnum;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.common.utils.poi.ExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据导出
 *
 * @author chong
 */
@RestController
@RequestMapping("/bm/monitor")
public class MonitorDataExcelController extends BaseController {


	@Autowired
	private MonitorDataExcelService excelService;


	/**
	 * 分页列表查询
	 * @param query
	 * @return
	 */
	@GetMapping(value = "/list")
	public TableDataInfo queryPageList(MonitorDataExcelBo query) {
		startPage();
		List<DataAnalysisVo> pageList = excelService.queryMonitorDataPageList(query);
		return getDataTable(pageList);
	}

	/**
	 * 导出
	 * @param response
	 * @param query
	 */
	@PostMapping(value = "/export")
	public void export(HttpServletResponse response , @RequestBody MonitorDataExcelBo query) {
		List<DataAnalysisVo> pageList = excelService.queryMonitorDataPageList(query);
		List<String> list = new ArrayList<>();
		list.add("设备编号");list.add("设备类型");list.add("采集时间");
//		String[] headers = {"设备编号","设备名称","设备类型","采集时间","采集值"};
		if(DeviceTypeEnum.DISTANCE.getType()==query.getMonitorType()){
			list.add("距离（mm）");list.add("挠度（mm）");
		}else if(DeviceTypeEnum.VIBRATIONACCELERATION.getType()==query.getMonitorType()){
			list.add("加速度（m/s）");list.add("频率（hz）");
		}else if(DeviceTypeEnum.TEMPERATURE.getType()==query.getMonitorType()){
			list.add("温度（℃）");
		} else if(DeviceTypeEnum.WEIGHT.getType()==query.getMonitorType()){
			list.add("车重（kg）");
		}else if(DeviceTypeEnum.MMH2O.getType()==query.getMonitorType()){
			list.add("沉降值（mm）");
		}
		else if(DeviceTypeEnum.DIPMONITOR.getType()==query.getMonitorType()){
			list.add("x轴（mm）");
			list.add("y轴（mm）");
			list.add("z轴（mm）");

		}
		String[] headers = list.toArray(new String[list.size()]);
		ExcelUtil.exportExcel(response, "monitor-data.xlsx", parseDataList(pageList), headers);
	}


	private List<List<Object>> parseDataList(List<DataAnalysisVo> records) {
		List<List<Object>> list = new ArrayList<>();
		if (CollectionUtils.isEmpty(records)){
			return list;
		}
		records.forEach(item -> {
			List<Object> itemList = new ArrayList<>();
			itemList.add(item.getSensorNo());
			DeviceTypeEnum em = DeviceTypeEnum.getEm(item.getSensorType());
			if(null!=em){
				itemList.add(em.getDesc());
			}else {
				itemList.add(null);
			}
			itemList.add(item.getCreatTime());
//			itemList.add(item.getMonitorValue());
			if(DeviceTypeEnum.DISTANCE.getType()==item.getSensorType()){
				itemList.add(item.getDistance());
				itemList.add(item.getDeflection());
			}else if(DeviceTypeEnum.VIBRATIONACCELERATION.getType()==item.getSensorType()){
				itemList.add(item.getAcceleration());
				itemList.add(item.getFrequency());
			}else if(DeviceTypeEnum.TEMPERATURE.getType()==item.getSensorType()){
				itemList.add(item.getTemperature());
			} else if(DeviceTypeEnum.WEIGHT.getType()==item.getSensorType()){
				itemList.add(item.getWeight());
			}else if(DeviceTypeEnum.MMH2O.getType()==item.getSensorType()){
				itemList.add(item.getJgValue());
			}else if(DeviceTypeEnum.WEIGHT.getType()==item.getSensorType()){
				itemList.add(item.getXval());
				itemList.add(item.getYval());
				itemList.add(item.getZval());
			}
			list.add(itemList);
		});
		return list;
	}
}
